package ru.spbu.math.android.cbrf.application;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import android.content.Context;
import android.content.SharedPreferences;
import android.util.Log;

public class AppConfigure {

	private static final String TAG = "App Widget Configure";

	private static final String PREFS_NAME = "ru.spbu.math.android.cbrf.application.AppProvider";
	private static final String SAVE_INDICATOR_NAME = "ru.spbu.math.android.cbrf.application.Indicator";
	private static final String PREF_PREFIX_KEY = "prefix_";
	private static final int MODE_PRIVATE = Context.MODE_PRIVATE;

	private static boolean indicator = false;

	static void saveIdRateType(Context context, String idRateType) {
		SharedPreferences.Editor prefs = context.getSharedPreferences(
				PREFS_NAME, MODE_PRIVATE).edit();
		int index = findEmptyIndex(context);
		prefs.putString(PREF_PREFIX_KEY + index, idRateType);
		prefs.commit();
		Log.d(TAG, "Save RateType with ID : " + idRateType);
		Log.d(TAG, "Commit save");
	}

	private static int findEmptyIndex(Context context) {
		Map<String, String> all = new HashMap<String, String>();
		all.putAll((Map<String, String>) loadAllTitlePrefs(context));
		int i = 1;
		while (all.containsKey(PREF_PREFIX_KEY + i)) {
			i++;
		}
		return i;
	}

	static void saveIsChecked(Context context, String idRateType,
			boolean indicator, int position) {
		String index = findIdStringByIdRateType(context, idRateType);
		Log.d(TAG, "SaveIsChecked.Index : " + index);
		Log.d(TAG, "SaveIsChecked.Indicator : " + indicator);
		SharedPreferences.Editor prefs = context.getSharedPreferences(
				SAVE_INDICATOR_NAME, MODE_PRIVATE).edit();
		prefs.putString(index, position + ":" + indicator);
		prefs.commit();
		Log.d(TAG, "isChecked was saved");
	}

	private static void parseCheckedString(Context context, String str) {
		if (str.indexOf(":") != -1) {
			String pos = str.substring(0, str.indexOf(":"));
			Log.d(TAG, "Pos : " + pos);
			String ind = str.substring(str.indexOf(":") + 1);
			Log.d(TAG, "Ind : " + ind);
			if(ind.equals("true"))
				indicator = true;
		}

	}

	static boolean isCkecked(Context context, String idRateType) {
		String index = findIdStringByIdRateType(context, idRateType);
		Log.d(TAG, "IsChecked.Index : " + index);
		if (index != null) {
			SharedPreferences prefs = context.getSharedPreferences(
					SAVE_INDICATOR_NAME, MODE_PRIVATE);
			parseCheckedString(context, prefs.getString(index, null));
			Log.d(TAG,
					"IsChecked.Indicator : " + indicator);
			return indicator;
		}
		return false;
	}

	static void deleteIdRateType(Context context, String idRateType) {
		SharedPreferences.Editor prefs = context.getSharedPreferences(
				PREFS_NAME, MODE_PRIVATE).edit();
		String index = findIdStringByIdRateType(context, idRateType);
		prefs.remove(index);
		prefs.commit();
	}

	static String findIdStringByIdRateType(Context context, String idRateType) {
		Map<String, String> all = new HashMap<String, String>();
		all.putAll((Map<String, String>) loadAllTitlePrefs(context));
		Log.d(TAG, "Search RateType with ID :" + idRateType);
		Iterator it = all.values().iterator();
		while (it.hasNext()) {
			Log.d(TAG, "Saved args : " + it.next());
		}
		Iterator<String> iterator = all.keySet().iterator();
		while (iterator.hasNext()) {
			String index = iterator.next();
			Log.d(TAG, "IdString : " + index);
			Log.d(TAG, "Id Saved Rate Type : " + all.get(index));
			if (all.get(index).equals(idRateType))
				return index;
		}
		return null;
	}

	static Map<String, ?> loadAllTitlePrefs(Context context) {
		SharedPreferences prefs = context.getSharedPreferences(PREFS_NAME,
				MODE_PRIVATE);
		return prefs.getAll();
	}

	static Map<String, ?> loadAllTitleIndicator(Context context) {
		SharedPreferences prefs = context.getSharedPreferences(
				SAVE_INDICATOR_NAME, MODE_PRIVATE);
		return prefs.getAll();
	}

	static void Empty(Context context) {
		SharedPreferences.Editor prefs = context.getSharedPreferences(
				PREFS_NAME, MODE_PRIVATE).edit();
		prefs.clear();
		prefs.commit();
	}

}
